# -*- coding: utf-8 -*-
# @Time : 2021/9/10 21:29
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : clue_page.py
# @Project : crm自动化测试项目实战
'''线索页面类'''

from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep
import time
from selenium.webdriver.support.select import Select
from utils.util import HOST2




class CluePage(BasePage):
    '''线索首页'''

    # 定义定位器：

    url = '{}/index.php?m=leads&a=index'.format(HOST2)
    clue_locator = (By.CSS_SELECTOR, 'body > div.container > div.page-header > ul > li:nth-child(1) > a')  # [线索]定位器
    clue_add_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')  # 添加线索定位器
    clue_checkall_locator = (By.ID, 'check_all')  # 全选线索定位器
    operation_locator = (
    By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > ul > li:nth-child(1) > div > a')  # 批量操作定位器
    delete_locator = (By.XPATH, '//*[@id="delete"]')  # 批量删除定位器
    clue_statistics_locator = (By.XPATH, '/html/body/div[5]/div[1]/ul/li[3]/a')  # 线索【统计】按钮
    total_clue_locator = (By.XPATH, '//*[@id="report_content"]/table/tfoot/tr/td[2]')  # 线索总计
    select_department_locator = (By.ID,'department')   #选择部门定位器
    select_role_locator = (By.ID,'role')    #选择人员定位器
    start_time_locator = (By.ID,'start_time')     #开始日期定位器
    end_time_locator = (By.ID,'end_time')       #结束日期
    search_locator = (By.CSS_SELECTOR,'#searchForm > ul > li:nth-child(4) > button')    #搜索按钮定位器


    # 操作元素方法：


    def addclue_click(self):
        '''点击新建线索'''
        element = self.driver.find_element(*self.clue_add_locator)
        element.click()

    def input_name(self,name):
        '''输入联系人姓名'''
        element = self.driver.find_element(By.XPATH, '//*[@id="contacts_name"]')
        element.send_keys(name)

    def submit_click(self):
        '''线索编辑界面，点击保存'''
        element = self.driver.find_element(By.NAME, 'submit')
        element.click()

    def getin_statistics(self):
        '''进入线索统计界面'''
        element = self.driver.find_element(*self.clue_statistics_locator)
        element.click()

    def get_total_clue(self):
        '''获取线索总数'''
        text = self.driver.find_element(*self.total_clue_locator).text
        return text

    def goback_cule(self):
        '''点击【线索】'''
        element0 = self.driver.find_element(*self.clue_locator)
        element0.click()   #点击【线索】回到线索首页

    def click_checkall(self):
        '''点击全选'''
        element = self.driver.find_element(*self.clue_checkall_locator)
        element.click()

    def click_operation(self):
        '''点击【批量操作】'''
        element1 = self.driver.find_element(*self.operation_locator)
        element1.click()

    def click_delete(self):
        '''点击删除'''
        element2 = self.driver.find_element(*self.delete_locator)
        element2.click()

    def delete_clue(self):
        '''批量删除线索'''
        self.click_checkall()
        self.click_operation()
        self.click_delete()
        self.alert()

        # 每条用例都要清理数据。。。。回到线索界面把添加的线索全部删掉



    def select_department(self,value='1'):
        '''选择部门--办公室'''
        locator = self.driver.find_element(*self.select_department_locator)
        select = Select(locator)
        select.select_by_value(value)


    def select_role(self,role='CEO-admin'):
        '''选择员工---admin'''
        locator = self.driver.find_element(*self.select_role_locator)    #给select的时候必须先找到他。
        select1 = Select(locator)
        sleep(3)
        select1.select_by_visible_text(role)

    def select_starttime(self):
        '''开始日期输入今天'''
        start_time_element = self.driver.find_element(*self.start_time_locator)
        today = time.strftime('%Y-%m-%d')  # 获取当前日期
        start_time_element.send_keys(today)
    def select_endtime(self):
        '''结束日期输入今天'''
        end_time_element = self.driver.find_element(*self.end_time_locator)
        today = time.strftime('%Y-%m-%d')  # 获取当前日期
        end_time_element.send_keys(today)

    def search(self):
        '''线索统计界面点击搜索'''
        self.driver.find_element(*self.search_locator).click()

